﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;
using System.Collections;


namespace WLC
{
    class WLCGetOrderStatisticStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCGetOrderStatisticStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition orderStatsElement = schema.AddElementProperty("OrderStatisticsElement", WLCOrderStatisticsElementDefinition.ID);
            orderStatsElement.Description = "The order statistics element that manages the statistics for the orders.";
            orderStatsElement.Required= true;
            
            IPropertyDefinition statisticName = schema.AddStringProperty("StatisticName", string.Empty);

            statisticName.Description = "The name of the statistic to get for the order.";
            statisticName.Required = true;

            IPropertyDefinition orderType = schema.AddStringProperty("OrderType", "current");
            orderType.Required = false;
            orderType.Description = "The order type for which to retrieve the statistic. If set to 'current' the type of the current associated object is used.";

            
            IPropertyDefinition saveToState = schema.AddStateProperty("SaveToState");
            saveToState.Description = "The state variable to save the order's statistic value to.";
            saveToState.Required = true;

            IPropertyDefinition valueType = schema.AddStringProperty("ValueType", "avg");
            valueType.Description = "The value type to return. Possible values are 'avg', 'min', 'max', or 'last'.";
            valueType.Required = false;

        }

        public int NumberOfExits
        {
            get { return 1; }
        }


        public string Name
        {
            get { return "GetOrderStatistic"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

       

        public Guid UniqueID
        {
            get { return new Guid("{3C8CDF38-1966-11DF-8826-5E2F56D89593}"); }
        }

        #endregion
    }


    class WLCGetOrderStatisticStep : IStep
    {

        IPropertyReader _statisticName;
        IPropertyReader _orderType;
        IStateProperty _saveToStateProp;
        IElementProperty _orderStatisticElementProp;
        IPropertyReader _valueTypeProp;
        

        public WLCGetOrderStatisticStep(IPropertyReaders bag)
        {
            _orderStatisticElementProp = (IElementProperty)bag.GetProperty("OrderStatisticsElement");
            _statisticName = bag.GetProperty("StatisticName");
            _orderType = bag.GetProperty("OrderType");
            _saveToStateProp = (IStateProperty)bag.GetProperty("SaveToState");
            _valueTypeProp = bag.GetProperty("ValueType");

    

        }

        #region IStep Members

        private string GetOrderType(IStepExecutionContext context)
        {
            string rawType = context.AssociatedObject.ExecutionContext.ToString();
            string[] splitType = rawType.Split(' ');

            
            return splitType[1];
        }

        public ExitType Execute(IStepExecutionContext context)
        {
            string orderType;
            // Get the order type from the associated object
            if (_orderType.GetStringValue(context).Equals("current"))
            {
                orderType = GetOrderType(context);
            }
            else orderType = _orderType.GetStringValue(context);

            string valueType = _valueTypeProp.GetStringValue(context);
            
            string statisticName = _statisticName.GetStringValue(context);
            
            WLCOrderStatisticsElement orderStatisticElement = (WLCOrderStatisticsElement)_orderStatisticElementProp.GetElement(context);

            //MessageBox.Show(orderStatisticElement.ToString());

            // Convert string to Enum
            WLCOrderStatisticsElement.ValueTypes valueTypeEnum = (WLCOrderStatisticsElement.ValueTypes)Enum.Parse(typeof(WLCOrderStatisticsElement.ValueTypes), valueType);

            //MessageBox.Show(orderType + " " + statisticName + " " + valueTypeEnum.ToString());

            double value = orderStatisticElement.GetStatisticValue(orderType, statisticName, valueTypeEnum);

            // Finally, set the value to the state variable to return it to Simio.
            _saveToStateProp.GetState(context).StateValue = value;

            return ExitType.FirstExit;
        }

        #endregion

    }


}
